﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fintech.Utils;
using ZOVNP.Model.Abstracts;
using ZOVNP.WinView.Workplaces.Seller;

namespace ZOVNP.WinView
{
    internal class SellerOrdersTabPage : SellerTabPage
    {
        private static IEnumerable<IOrdersTreePopulationStrategy> _ordersTreePopulationStrategies;

        private ComboBox _cbOrdersPopulationStrategies;
        private SplitContainer _scHoriz;
        private TableLayoutPanel _tlp;
        private TreeView _tvOrders;
        private SellerMainControl _mainControl;
        private IOrder[] _orders;

        public SellerOrdersTabPage(SellerMainControl mainControl) : base(mainControl)
        {
            Text = "Заказы";
            Name = "tpOrders";

            _mainControl = mainControl;

            var list = new List<IOrdersTreePopulationStrategy>();
            list.Add(new PlainOrdersTreePopulationStrategy());
            list.Add(new MonthlyOrdersTreePopulationStrategy());
            list.Add(new CustomerOrdersTreePopulationStrategies());
            _ordersTreePopulationStrategies = list.ToArray();
        }

        protected override void InitializeInner()
        {
            _cbOrdersPopulationStrategies = new ComboBox();
            _cbOrdersPopulationStrategies.Name = "cbOrdersPopulationStrategies";
            _cbOrdersPopulationStrategies.Dock = DockStyle.Fill;

            _cbOrdersPopulationStrategies.SelectedIndexChanged += CbOrdersPopulationStrategiesSelectedIndexChanged;
            
            _scHoriz = new SplitContainer();
            _scHoriz.Name = "scHoriz";
            _scHoriz.Orientation = Orientation.Vertical;
            _scHoriz.Dock = DockStyle.Fill;
            _scHoriz.FixedPanel = FixedPanel.Panel1;
            this.Controls.Add(_scHoriz);

            _tlp = new TableLayoutPanel();
            _tlp.ColumnCount = 1;
            _tlp.RowCount = 2;
            _tlp.Dock = DockStyle.Fill;
            _tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
            _tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            _tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
            _scHoriz.Panel1.Controls.Add(_tlp);

            _tvOrders = new TreeView();
            _tvOrders.Name = "tvOrders";
            _tvOrders.Dock = DockStyle.Fill;
            _tvOrders.Width = 700;
            _tlp.Controls.Add(_tvOrders);
            _tlp.Controls.Add(_cbOrdersPopulationStrategies, 0, 0);

            _tlp.SetColumn(_tvOrders, 0);
            _tlp.SetRow(_tvOrders, 1);
            _tlp.SetColumnSpan(_tvOrders, 1);
            _tlp.SetRowSpan(_tvOrders, 1);

            _tlp.SetColumn(_cbOrdersPopulationStrategies, 0);
            _tlp.SetRow(_cbOrdersPopulationStrategies, 0);
            _tlp.SetColumnSpan(_cbOrdersPopulationStrategies, 1);
            _tlp.SetRowSpan(_cbOrdersPopulationStrategies, 1);

            _scHoriz.SplitterDistance = 1000;

            _tvOrders.AfterSelect += TvOrdersAfterSelect;

            // Устанавливаем источник данных для комбобокса.
            // SelectedIndex устанавливается автоматически => вызывается загрузка значений.
            _cbOrdersPopulationStrategies.DataSource = _ordersTreePopulationStrategies;
        }

        private void TvOrdersAfterSelect(object sender, TreeViewEventArgs e)
        {
            OrderNode orderNode = e.Node as OrderNode;
            if(orderNode != null)
            {
                _scHoriz.Panel2.Controls.Clear();
                var tlp = new TableLayoutPanel();
                tlp.ColumnCount = 1;
                tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
                tlp.RowCount = 2;
                tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
                tlp.Dock = DockStyle.Fill;

                var orderControl = new OrderEditControl(orderNode.Order, _mainControl.Workspace, true);
                orderControl.Dock = DockStyle.Fill;
                tlp.Controls.Add(orderControl, 0, 0);

                var orderBodyViewControl = new OrderBodyViewControl(orderNode.Order);
                orderBodyViewControl.Dock = DockStyle.Fill;
                tlp.Controls.Add(orderBodyViewControl, 0, 1);

                _scHoriz.Panel2.Controls.Add(tlp);
            }
        }

        public override void RefreshData()
        {
            LoadOrders();
        }

        public override void Add()
        {
            var frm = new AddOrderFullForm(_mainControl.Workspace);
            frm.Show(this);
        }

        public override void Delete()
        {

        }

        public override void Print()
        {

        }

        public override void Edit()
        {
            if (_tvOrders.SelectedNode != null)
            {
                var orderNode = (OrderNode) _tvOrders.SelectedNode;
                var frm = new EditOrderFullForm(_mainControl.Workspace, orderNode.Order);
                frm.Show(this);
            }
        }

        private void LoadOrders()
        {
            _tvOrders.Nodes.Clear();
            if (_cbOrdersPopulationStrategies.SelectedItem != null)
            {
                List<OrderNode> nodes = new List<OrderNode>();
                var orders = _mainControl.Workspace.GetAllOrders();
                orders.ForEach(o =>
                                   {
                                       var context = _mainControl.Workspace.Engine.CreateContext();
                                       context.AttachObject(o);
                                       nodes.Add(new OrderNode(o));
                                   });
                _orders = orders.ToArray();
                ((IOrdersTreePopulationStrategy) (_cbOrdersPopulationStrategies.SelectedItem)).PopulateTree(_tvOrders, nodes);

            }
        }

        private void CbOrdersPopulationStrategiesSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadOrders();
        }

        public override void Save()
        {
            
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            _orders.ForEach(o =>
                                {
                                    if (o.Context != null)
                                        o.Context.Dispose();
                                });
        }
    }
}
